using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.UI;

public class TwoThreePanel : MonoBehaviour
{
    public TwoThreeItem item;
    public Transform ItemGroup;
    List<TwoThreeData> dataList = new List<TwoThreeData>();

    List<TwoThreeItem> items = new List<TwoThreeItem>();
    public Image PlayerImage;
    public Text PlayerText;
    int my_Index = 0;
    // Start is called before the first frame update
    void Start()
    {
        dataList = JsonConvert.DeserializeObject<List<TwoThreeData>>(Resources.Load<TextAsset>("TwoThreeData").text);

        MessMgr.GetSinleton().AddListener("TwoThreePanel", refreshData);

        for (int i = 0; i < dataList.Count; i++)
        {
            var temp = Instantiate(item, ItemGroup);

            temp.gameObject.SetActive(true);
            items.Add(temp);
            items[i].GetComponent<RectTransform>().localPosition = new Vector3(dataList[i].x, dataList[i].y, 0);
        }
        refreshData();
        item.gameObject.SetActive(false);
    }
    private void OnEnable()
    {
        if (!item.gameObject.activeSelf)
        {
            refreshData();
        }
    }
    public void refreshData()
    {
        for (int i = 0; i < dataList.Count; i++)
        {
            items[i].init(dataList[i]);
            if (dataList[i].aimNum <= TwoPanel.Ins.MoneyNum)
            {
                my_Index = i;
            }
        }
        if (dataList[my_Index].x > 0)
        {
            PlayerImage.GetComponent<RectTransform>().localPosition = new Vector3(dataList[my_Index].x, dataList[my_Index].y, 0) + new Vector3(-100, 0, 0);
        }
        else
        {
            PlayerImage.GetComponent<RectTransform>().localPosition = new Vector3(dataList[my_Index].x, dataList[my_Index].y, 0) + new Vector3(100, 0, 0);
        }
        PlayerText.text = "当前进度: " + TwoPanel.Ins.MoneyNum;
    }
    // Update is called once per frame
    void Update()
    {

    }
}
public class TwoThreeData
{
    public int aimNum;
    public int x;
    public int y;
    public int freeState;
    public int MoneyState;
    public int free;
    public int Money;
}
